package com.sunmouse.toolkit.util;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.provider.ContactsContract;

import com.sunmouse.toolkit.constant.Config;
import com.sunmouse.toollib.utils.system.PermissionUtil;

import java.util.List;

/**
 * @Author Kvin
 * @Email: 1746861438@qq.com
 * @data 2018/8/2 15:56
 */
public class IntentUtil {

    private Activity mAct;

    private IntentUtil(Activity act) {
        this.mAct = act;
    }

    public static IntentUtil getInstance(Activity act) {
        return new IntentUtil(act);
    }

    /**
     * call
     *
     * @param no
     */
    public void call(String no, int requestCode) {
        if (PermissionUtil.isCallPermitted(mAct, requestCode)) {
            Uri number = Uri.parse("tel:" + no);
            Intent intent = new Intent(Intent.ACTION_CALL, number);
            if (!isIntentSafe(intent)) return;
            this.mAct.startActivity(intent);
        }
    }

    /**
     * access map
     */
    public void accessMap(String address, int requestCode) {
//        if (PermissionUtil.isLocationPermitted(mAct, requestCode)) {
            Uri addr = Uri.parse(address);
            Intent intent = new Intent(Intent.ACTION_VIEW, addr);
            if (!isIntentSafe(intent)) return;
            this.mAct.startActivity(intent);
//        }
    }

    /**
     * access web
     */
    public void accessWeb(String url) {
        Uri page = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, page);
        if (!isIntentSafe(intent)) return;
        this.mAct.startActivity(intent);
    }

    /**
     * send Email
     */
    public void sendEmail(String to, String eSub, String eDec, String stream) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
        intent.putExtra(Intent.EXTRA_SUBJECT, eSub);
        intent.putExtra(Intent.EXTRA_TEXT, eDec);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(stream));
        if (!isIntentSafe(intent)) return;
        mAct.startActivity(intent);
    }

    /**
     * access contract
     */
    public void accessContract(int requestCode) {
        if (PermissionUtil.isReadingContractPermitted(mAct, requestCode)) {
            Uri uri = Uri.parse("content://contracts");
            Intent intent = new Intent(Intent.ACTION_PICK, uri);
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
            if (!isIntentSafe(intent)) return;
            mAct.startActivityForResult(intent, requestCode);
        }
    }

    /**
     * launch  target app
     */
    public void launchTarget(String action){
        Intent mIntent=new Intent(action);
        mIntent.setType(Config.MimeType.TEXT);
        if (!isIntentSafe(mIntent)) return;
        mAct.startActivity(mIntent);
    }


    /**
     * check whether the intent is safe
     */
    private boolean isIntentSafe(Intent intent) {
        List<ResolveInfo> list = mAct.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }
}
